Page({
  data: {
    passport: {}
  },
  onLoad({ id, encryptedData }) {
    this.getPassport(id)
    this.getPassportShare(encryptedData)
  },
  async getPassportShare(encryptedData) {
    if (!encryptedData) return
    const { code, data } = await wx.http.get(`/visitor/share/${encryptedData}`)
    if (code !== 10000) return wx.utils.toast()
    // console.log(data)
    this.setData({
      passport: data
    })
  },
  async getPassport(id) {
    if (!id) return
    const { code, data } = await wx.http.get(`/visitor/${id}`)
    if (code !== 10000) return wx.utils.toast()
    // console.log(data)
    this.setData({
      passport: data
    })
  },
  // 此页面可分享
  onShareAppMessage() {
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + this.data.passport.encryptedData,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
  async save() {
    const res = await wx.getImageInfo({
      src: this.data.passport.url,
    })
    // console.log(res)
    await wx.saveImageToPhotosAlbum({
      filePath: res.path,
    })
  }
})
